package com.wish.group05.finance.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.CwCheckBatchDetail;
import com.wish.group05.base.entity.CwPayOrder;
import com.wish.group05.base.entity.CwPayOrderDetail;
import com.wish.group05.base.service.ICwCheckBatchDetailService;
import com.wish.group05.base.service.ICwPayOrderDetailService;
import com.wish.group05.base.service.ICwPayOrderService;
import com.wish.group05.finance.dto.CwPayOrderDto;
import com.wish.group05.system.api.RemoteDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 支付订单（支付流水信息）Controller
 * 
 * @author wish
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/order")
public class TCwPayOrderController extends BaseController
{
    @Autowired
    ICwPayOrderService cwPayOrderService;
    @Resource
    RemoteDictDataService remoteDictDataService;
    @Autowired
    ICwPayOrderDetailService cwPayOrderDetailService;

    @GetMapping("getDict")
    public AjaxResult getDict() {
        HashMap<String, Object> map = new HashMap<>();
        //交易账户类型集合
        AjaxResult tradeAccountType = remoteDictDataService.getDictDataList("sys_trade_account_type");
        map.put("tradeAccountTypeList",tradeAccountType);
        //用户类型
        AjaxResult userType = remoteDictDataService.getDictDataList("sys_user_type");
        map.put("userTypeList",userType);
        //交易结果
        AjaxResult tradeResult = remoteDictDataService.getDictDataList("sys_trade_result");
        map.put("tradeResultList",tradeResult);
        //支付方式
        AjaxResult payWay = remoteDictDataService.getDictDataList("sys_pay_way");
        map.put("payWayList",payWay);
        return AjaxResult.success(map);
    }

    /**
     * 查询支付订单（支付流水信息）列表
     */
    @PreAuthorize(hasPermi = "system:order:list")
    @GetMapping("/list")
    public TableDataInfo list(CwPayOrderDto cwPayOrderDto)
    {
        QueryWrapper<CwPayOrder> wrapper = new QueryWrapper<>();

        //交易流水号查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getOrderNo())) {
            wrapper.like("order_no",cwPayOrderDto.getOrderNo());
        }
        //结算金额查询
        if (cwPayOrderDto.getFinalAmount()!=null) {
            wrapper.eq("final_amount",cwPayOrderDto.getFinalAmount());
        }
        //支付金额查询
        if (cwPayOrderDto.getPayAmount()!=null) {
            wrapper.eq("pay_amount",cwPayOrderDto.getPayAmount());
        }
        //业务订单号查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getPayOrderNo())) {
            wrapper.like("pay_order_no",cwPayOrderDto.getPayOrderNo());
        }
        //交易对手查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getTradeUser())) {
            wrapper.like("trade_user",cwPayOrderDto.getTradeUser());
        }
        //发起交易日期查询
        if (cwPayOrderDto.getTradeDateFq()!=null) {
            wrapper.ge("trade_date_fq",cwPayOrderDto.getTradeDateFq());
        }
        //发起交易日期查询
        if (cwPayOrderDto.getTradeDateFq1()!=null) {
            wrapper.le("trade_date_fq",cwPayOrderDto.getTradeDateFq1());
        }
        //交易账户查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getTradeAccountNo())) {
            wrapper.like("trade_account_no",cwPayOrderDto.getTradeAccountNo());
        }
        //对手账户查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getDsAccount())) {
            wrapper.like("ds_account",cwPayOrderDto.getDsAccount());
        }
        //发起交易日期查询
        if (cwPayOrderDto.getTradeDateJz()!=null) {
            wrapper.ge("trade_date_jz",cwPayOrderDto.getTradeDateJz());
        }
        //发起交易日期查询
        if (cwPayOrderDto.getTradeDateJz1()!=null) {
            wrapper.le("trade_date_jz",cwPayOrderDto.getTradeDateJz1());
        }
        //交易账户查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getTradeAccountNo())) {
            wrapper.like("trade_account_no",cwPayOrderDto.getTradeAccountNo());
        }
        //交易渠道查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getTradeQudao())) {
            wrapper.like("trade_qudao",cwPayOrderDto.getTradeQudao());
        }
        //交易账户类型查询
        if (cwPayOrderDto.getTradeAccountType()!=null) {
            wrapper.eq("trade_account_type",cwPayOrderDto.getTradeAccountType());
        }
        //渠道订单号查询
        if (StringUtils.isNoneBlank(cwPayOrderDto.getQudaoOrderNo())) {
            wrapper.like("qudao_order_no",cwPayOrderDto.getQudaoOrderNo());
        }
        //用户类型查询
        if (cwPayOrderDto.getUserType()!=null) {
            wrapper.eq("user_type",cwPayOrderDto.getUserType());
        }
        //交易结果查询
        if (cwPayOrderDto.getTradeResult()!=null) {
            wrapper.eq("trade_result",cwPayOrderDto.getTradeResult());
        }
        //支付方式查询
        if (cwPayOrderDto.getPayWay()!=null) {
            wrapper.eq("pay_way",cwPayOrderDto.getPayWay());
        }
        //订单金额查询
        if (cwPayOrderDto.getAmount()!=null) {
            wrapper.eq("amount",cwPayOrderDto.getAmount());
        }
        wrapper.orderByDesc("trade_date_fq");
        wrapper.eq("del_flg",0);
        startPage();
        List<CwPayOrder> list = cwPayOrderService.list(wrapper);
        for (CwPayOrder cwPayOrder : list) {
            //根据订单号查询详情对象
            List<CwPayOrderDetail> detailList = cwPayOrderDetailService.list(new QueryWrapper<CwPayOrderDetail>().
                    eq("pay_order_id", cwPayOrder.getId()));
            if (detailList != null || detailList.size() > 0) {
                for (CwPayOrderDetail cwPayOrderDetail : detailList) {
                    cwPayOrder.setPayOrderNo(cwPayOrderDetail.getPayOrderNo());
                }
            }


        }
        return getDataTable(list);
    }

//    /**
//     * 导出支付订单（支付流水信息）列表
//     */
//    @PreAuthorize(hasPermi = "system:order:export")
//    @Log(title = "支付订单（支付流水信息）", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TCwPayOrder tCwPayOrder) throws IOException
//    {
//        List<TCwPayOrder> list = tCwPayOrderService.selectTCwPayOrderList(tCwPayOrder);
//        ExcelUtil<TCwPayOrder> util = new ExcelUtil<TCwPayOrder>(TCwPayOrder.class);
//        util.exportExcel(response, list, "order");
//    }
//
//    /**
//     * 获取支付订单（支付流水信息）详细信息
//     */
//    @PreAuthorize(hasPermi = "system:order:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return AjaxResult.success(tCwPayOrderService.selectTCwPayOrderById(id));
//    }
//
//    /**
//     * 新增支付订单（支付流水信息）
//     */
//    @PreAuthorize(hasPermi = "system:order:add")
//    @Log(title = "支付订单（支付流水信息）", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody TCwPayOrder tCwPayOrder)
//    {
//        return toAjax(tCwPayOrderService.insertTCwPayOrder(tCwPayOrder));
//    }
//
//    /**
//     * 修改支付订单（支付流水信息）
//     */
//    @PreAuthorize(hasPermi = "system:order:edit")
//    @Log(title = "支付订单（支付流水信息）", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody TCwPayOrder tCwPayOrder)
//    {
//        return toAjax(tCwPayOrderService.updateTCwPayOrder(tCwPayOrder));
//    }
//
//    /**
//     * 删除支付订单（支付流水信息）
//     */
//    @PreAuthorize(hasPermi = "system:order:remove")
//    @Log(title = "支付订单（支付流水信息）", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(tCwPayOrderService.deleteTCwPayOrderByIds(ids));
//    }
}
